﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraScheduler;

namespace QLTV.UserControl
{
    public partial class ucLongPlan : DevExpress.XtraEditors.XtraUserControl
    {
        public ucLongPlan()
        {
            InitializeComponent();
            loadData();
        }

        public void loadData()
        {
            this.tblVesselTableAdapter.Fill(this.dsLongPlan.tblVessel);
            // TODO: This line of code loads data into the 'dsLongPlan.tblRank' table. You can move, or remove it, as needed.
            this.tblRankTableAdapter.Fill(this.dsLongPlan.tblRank);
            // TODO: This line of code loads data into the 'dsLongPlan.tblLongPlanVessel' table. You can move, or remove it, as needed.
            this.tblLongPlanVesselTableAdapter.Fill(this.dsLongPlan.tblLongPlanVessel);
            this.schedulerStorage1.Appointments.CustomFieldMappings.Add(new AppointmentCustomFieldMapping("VesselID", "nVesselId"));
            this.schedulerStorage1.Appointments.CustomFieldMappings.Add(new AppointmentCustomFieldMapping("CrewID", "nCrewId"));
            this.schedulerStorage1.Appointments.CustomFieldMappings.Add(new AppointmentCustomFieldMapping("RankID", "nRankId"));
        }

        private void schedulerControl1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e)
        {
            if (e.Menu.Id == DevExpress.XtraScheduler.SchedulerMenuItemId.DefaultMenu)
            {
                e.Menu.RemoveMenuItem(SchedulerMenuItemId.NewAllDayEvent);
                e.Menu.RemoveMenuItem(SchedulerMenuItemId.NewRecurringAppointment);
                e.Menu.RemoveMenuItem(SchedulerMenuItemId.SwitchViewMenu);
                e.Menu.RemoveMenuItem(SchedulerMenuItemId.SwitchTimeScale);
                e.Menu.RemoveMenuItem(SchedulerMenuItemId.TimeScaleVisible);
                e.Menu.RemoveMenuItem(SchedulerMenuItemId.TimeScaleEnable);

                SchedulerMenuItem item = e.Menu.GetMenuItemById(SchedulerMenuItemId.NewAppointment);
                if (item != null) item.Caption = ((BaseForm)ParentForm).rm.GetString("sPlan", ((BaseForm)ParentForm).myCultureInfo);

                #region them mot command
                // Create a menu item for the Scheduler command.
                //ISchedulerCommandFactoryService service =
                //(ISchedulerCommandFactoryService)schedulerControl1.GetService(typeof(ISchedulerCommandFactoryService));
                //SchedulerCommand cmd = service.CreateCommand(SchedulerCommandId.SwitchToGroupByResource);
                //SchedulerMenuItemCommandWinAdapter menuItemCommandAdapter =
                //    new SchedulerMenuItemCommandWinAdapter(cmd);
                // Insert a new item into the Scheduler popup menu and handle its click event.
                //   e.Menu.Items.Add(new SchedulerMenuItem("Click me!", MyClickHandler));

                //DXMenuItem menuItem = new DXMenuItem("Lập kế hoạch", MyClickHandler) { BeginGroup = true };
                //e.Menu.Items.Add(menuItem);

                //e.Menu.MoveMenuItem(menuItem, 0); 
                #endregion
            }
        }

        private void schedulerStorage1_AppointmentsChanged(object sender, PersistentObjectsEventArgs e)
        {
            this.tblLongPlanVesselTableAdapter.Update(dsLongPlan);
            dsLongPlan.AcceptChanges();
            schedulerControl1.Refresh();
        }

        private void schedulerControl1_EditAppointmentFormShowing(object sender, AppointmentFormEventArgs e)
        {
            Appointment apt = e.Appointment;
            DevExpress.XtraScheduler.SchedulerControl control = (DevExpress.XtraScheduler.SchedulerControl)sender;
            if (control.Storage.Appointments.IsNewAppointment(apt) == true)
            {
                if (lookupVessel.EditValue != null)
                {
                    apt.CustomFields["VesselID"] = (int)lookupVessel.EditValue;
                    apt.CustomFields["CrewID"] = "";
                }
                else
                {
                    XtraMessageBox.Show(((BaseForm)ParentForm).rm.GetString("sVesselChoice", ((BaseForm)ParentForm).myCultureInfo), ((BaseForm)ParentForm).rm.GetString("sMessageCaption", ((BaseForm)ParentForm).myCultureInfo), MessageBoxButtons.OK);
                    lookupVessel.Focus();
                    e.Handled = true;
                    return;
                }
            }
            // Create a custom form.
            Forms.KeHoach.frmEditPlanPopup myForm = new Forms.KeHoach.frmEditPlanPopup((SchedulerControl)sender, apt, false);
            try
            {
                // Required for skins support.
                myForm.LookAndFeel.ParentLookAndFeel = schedulerControl1.LookAndFeel;
                e.DialogResult = myForm.ShowDialog();
                schedulerControl1.Refresh();
                e.Handled = true;
                this.tblLongPlanVesselTableAdapter.Update(dsLongPlan);
                dsLongPlan.AcceptChanges();

            }
            finally
            {
                myForm.Dispose();
            }

        }

        private void schedulerStorage1_AppointmentsDeleted(object sender, PersistentObjectsEventArgs e)
        {
            this.tblLongPlanVesselTableAdapter.Update(dsLongPlan);
            dsLongPlan.AcceptChanges();
            schedulerControl1.Refresh();
        }
    }
}